home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5172 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: odi.com!usenet
  2. From: Kimberley Burchett <burchett>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Visibility Restricted to Compiler
  5. Date: 2 Feb 1996 20:49:20 GMT
  6. Organization: Software Leverage, Inc
  7. Message-ID: <4ettcg$d1f@mastermind.odi.com>
  8. References: <4eq8td$hpu@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: loon.odi.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3 sun4c)
  14. X-URL: news:4eq8td$hpu@vixen.cso.uiuc.edu
  15.  
  16. sjmccaug@prairienet.org (Scott J. McCaughrin) wrote:
  17. >
  18. > Is a constructor still visibile to a C++ compiler if it is private? I
  19. > want a constructor invoked only by compiler, and not e.g. by some
  20. > user-defined 'new'.
  21.  
  22.   If the constructor is private, it can only be called by the class itself.
  23. It can't even be called implicitly by new.  For example, a private constructor
  24. means you cannot do this:
  25.  
  26. void foo()
  27. {
  28.   Bar bar;           // can't construct bar because bar::bar is private
  29.   Bar* barptr;       // okay -- constructor not called yet
  30.   barptr = new Bar;  // not okay -- can't call bar::bar
  31. }
  32.  
  33.   The only reasons I can think of that you'd want a private constructor are
  34. 1) your class provides a bunch of static member functions, but isn't meant
  35. to be instantiated, or 2) your class uses some other member function to 
  36. create the object -- for example a function named "create" that returns a
  37. pointer or reference to an object.  This last example is often used when
  38. you really want to return a pointer to some derived class.
  39.  
  40. Kimberley
  41.  
  42.